#pragma once
#include<iostream>
#include<unistd.h>
using namespace std;

typedef void(*work_t)(int);
typedef void(*task_t)(int,pid_t);

void printlog(int fd,pid_t pid)
{
    cout <<"sub pro:" << pid <<" fd:" << fd << " printlog_task" <<endl;
}

void ConnectMysql(int fd,pid_t pid)
{
    cout <<"sub pro:" << pid <<" fd:" << fd << " printConnectMsql_task" << endl;
}

void reloade(int fd,pid_t pid)
{
    cout <<"sub pro:" << pid <<" fd:" << fd << " printreloade_task" << endl;
}

task_t task[3] = {printlog,ConnectMysql,reloade};

uint32_t Next()
{
    return rand() % 3;
}

void worker(int fd)
{
    //从文件描述符0==键盘中读取任务
    while(true)
    {
        uint32_t command_code = 0; //无符号整数
        ssize_t n = read(0, &command_code, sizeof(command_code));
        if(n == sizeof(command_code))
        {   
            if(command_code >= 3) continue;
            task[command_code](fd,getpid());        
        }
        else if( n == 0)
        {
            cout << "sub process: " << getpid() << endl;
            break;
        }
//      cout << "i am worker:" << getpid() <<endl;
    }

}